package tts.dao;

// Generated 10-dic-2013 12.19.27 by Hibernate Tools 4.0.0

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import tts.model.Groups;
import tts.model.Users;

/**
 * Home object for domain model class Groups.
 * @see .Groups
 * @author Hibernate Tools
 */
@Repository("groupsHome")
@Transactional(propagation = Propagation.REQUIRED)
public class GroupsHome {

	private static final Log log = LogFactory.getLog(GroupsHome.class);

	@PersistenceContext
	private EntityManager entityManager;

	public void insert(Groups transientInstance) {
		log.debug("persisting Groups instance");
		try {
			entityManager.persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public Groups update(String groupName, Groups newGroup) throws RuntimeException {
		log.info("updating Groups instance: " + groupName);
		try {
			Groups result = this.findByGroupName(groupName);
			result.setGroupName(newGroup.getGroupName());
			result.getUsersList().clear();
			for (Users u : newGroup.getUsersList()) {
				result.getUsersList().add(u);
			}
			log.info("update successful");
			return result;
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public void remove(Groups persistentInstance) {
		log.debug("removing Groups instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Groups merge(Groups detachedInstance) {
		log.debug("merging Groups instance");
		try {
			Groups result = entityManager.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Groups findById(Integer id) {
		log.debug("getting Groups instance with id: " + id);
		try {
			Groups instance = entityManager.find(Groups.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public Collection<Groups> findAll() throws RuntimeException {
		log.info("getting all groups in db");
		try {
			Collection<Groups> list = entityManager.createQuery("from Groups order by groupName").getResultList();
			log.info("get successful");
			return list;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public Groups findByGroupName(String groupName) {
		log.debug("getting Groups instance "+ groupName);
		try {
			Groups instance = (Groups) entityManager.createQuery("from Groups where groupName='"+ groupName +"'").getResultList().get(0);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
}